package cn.nuc.wwb_1248.controller;

import cn.nuc.wwb_1248.proj.wwbBook;
import cn.nuc.wwb_1248.proj.service.wwbBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
//@RequestMapping("/wwb")
public class WwbBookController {
    static String paths = null;
    @Autowired
    private wwbBookService wwbBookService;
    private wwbBook wwbBook;

    @RequestMapping("/wwbFindBookAll")
    @ResponseBody  //转化为JSON格式返回数据
    public List<wwbBook> getAllBook(){
        return wwbBookService.findAllBook();
    }

    @RequestMapping("/wwbFindBook")
    @ResponseBody
    public wwbBook getBook(@RequestBody String id){
        return wwbBookService.getBook(id);
    }

    @RequestMapping("/wangwenboFindOne/{id}")
    @ResponseBody
    public ModelAndView getBookModel(@PathVariable("id") String id){
        wwbBook wwbBookInfo = wwbBookService.getBook(id);
        ModelAndView mav = new ModelAndView();
//        模型图需要生成JSON视图
        MappingJackson2JsonView mjv = new MappingJackson2JsonView();
        mav.setView(mjv);
        mav.addObject("wwbBook",wwbBookInfo);
        return mav;
    }

    @RequestMapping("/AddBook")
    public String AddBook(){
        return "/wangwenbo/AddBook";
    }

    @RequestMapping("wwbInsertBook")
    @ResponseBody
    public wwbBook insertBook(@RequestBody wwbBook wwbBook){
        wwbBook.setwwbPath(paths);
        System.out.println(wwbBook.toString());
        wwbBookService.insertBook(wwbBook);
        return wwbBook;
    }

    @RequestMapping("/Details")
    public ModelAndView findAllBook(){
        ModelAndView mav = new ModelAndView();
        List<wwbBook> books = new ArrayList<wwbBook>();
        books = wwbBookService.findAllBook();
        mav.setViewName("/wangwenbo/Details");
        mav.addObject("books",books);
        return mav;
    }

    @RequestMapping("wwb/detail.json/{id}")
    @ResponseBody
    public wwbBook findOneBookJson(@PathVariable("id") String id){
        return wwbBookService.getBook(id);
    }


    @PostMapping("/upload/wwbMultipart")
    @ResponseBody
//    使用Spring MVC的MultipartFile类作为参数，对应了保存的临时文件
    public ModelAndView upload(@RequestParam("wwbCover") MultipartFile photo1){
        String path = "F:\\wwb_1248\\src\\main\\webapp\\img\\";
        String fileNmae = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        System.out.println(photo1.getOriginalFilename());
        String suffix = photo1.getOriginalFilename().substring(photo1.getOriginalFilename().lastIndexOf("."));
        paths = "img/"+fileNmae + suffix;
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/wangwenbo/Details");
        if (suffix.equals(".jpg")||suffix.equals(".png")){
            try {
                //Spring提供了文件操作类FileCopyUtils
                FileCopyUtils.copy(photo1.getInputStream(),new FileOutputStream(path+fileNmae+suffix));
                return mav;
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return null;
    }
    @GetMapping("/uploads")
    public String uploadPages(){
        return "/wwbFile/wwbUploads";
    }
    @PostMapping("/upload/wwbMultiparts")
    @ResponseBody
    //使用Spring MVC的MultipartFile类作为参数，对应了保存的临时文件
    public Map<String,Object> uploads(@RequestParam("photo") MultipartFile [] photos){
        String path = "E:/upload/";
        System.out.println(photos.length);
        for (MultipartFile photo: photos){
            if (!photo.isEmpty()){
                String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
                String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf("."));
                if (!suffix.equals(".jpg")&&!suffix.equals(".png")){
                    return dealResultMap(false,"上传失败");
                }
                try {
                    FileCopyUtils.copy(photo.getInputStream(),new FileOutputStream(path+fileName+suffix));
                }catch (IOException e) {
                    e.printStackTrace();
                    return dealResultMap(false, "上传失败");
                }
            }
        }
        return dealResultMap(true,"上传成功");
    }


    //保存文件
    private Map<String,Object> dealResultMap(boolean success,String msg){
        Map<String,Object> result = new HashMap<>();
        result.put("result",success);
        result.put("msg",msg);
        return result;
    }
}